package be.ac.ua.customer;

import java.io.IOException;
import java.rmi.RMISecurityManager;
import java.util.UUID;
import java.util.Vector;

import distrcomp.Address;

import net.jini.config.ConfigurationException;
import net.jini.core.entry.Entry;
import net.jini.core.lease.LeaseDeniedException;
import net.jini.core.transaction.CannotCommitException;
import net.jini.core.transaction.CannotJoinException;
import net.jini.core.transaction.UnknownTransactionException;
import net.jini.core.transaction.server.CrashCountException;
import net.jini.space.JavaSpace;
import be.ac.ua.codebase.CustomerInterface;
import be.ac.ua.codebase.DutchAuction;
import be.ac.ua.codebase.EnglishAuction;
import be.ac.ua.codebase.Product;
import be.ac.ua.codebase.VickreyAuction;

public class CustomerTest {
	
	public static void main(String[] args) {
		CustomerImplementation customer1 = null;
		CustomerImplementation customer2 = null;
		
		try {
			System.setProperty("java.security.policy", "resources/policies.policy");
			if(args.length > 0) {
				System.setProperty("java.rmi.server.codebase", args[0]);
			} else {
				System.setProperty("java.rmi.server.codebase", "http://localhost:8080/");
			}
			System.setSecurityManager(new RMISecurityManager());
			
			Address address1 = new Address("Middelheimlaan","1","38115","Antwerp","US");
			Address address2 = new Address("Pauline Vanier","8001","H1J2V8","Anjou","CA");
			customer1 = new CustomerImplementation("Niel", "Deckx", address1);
			customer2 = new CustomerImplementation("Jens", "Van Duyse", address2);
			Thread.sleep(1000);
			customer1.registerWithBank();
			customer2.registerWithBank();
			customer1.getBank().credit(customer1.getBankAccountID(), 2000);
			customer2.getBank().credit(customer2.getBankAccountID(), 2000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			//Export customers and get a proxy
			CustomerInterface customer1Proxy = customer1.export();
			CustomerInterface customer2Proxy = customer2.export();
			
			//Products
			Product product1 = new Product("iPhone");
			Product product2 = new Product("Macbook");
			Product product3 = new Product("MacbookPro");
			
			//Provider
			customer1.offerProduct(product1, new Long(20*1000), null, null, VickreyAuction.class);
			customer1.offerProduct(product2, new Long(20*1000), new Double(300), new Double(1200), VickreyAuction.class);
			customer1.offerProduct(product3, new Long(20*1000), null, null, VickreyAuction.class);
			
			Vector<Product> searchResult = customer2.getStoreConnector().search("Macbook");
			for (int i = 0; i < searchResult.size(); i++) {
				System.out.println("Search result: " + searchResult.get(i).getName());
			}
			
			//Join a bidding
			if(searchResult.size() > 0) {
				customer1.joinBidding(searchResult.get(0).getID());
				customer2.joinBidding(searchResult.get(0).getID());
				Thread.sleep(8000);
				double amount = 925.99;
				customer2.placeBidding(searchResult.get(0).getID(), amount);
				/*
				Thread.sleep(1000);
				amount = 479.99;
				customer2.placeBidding(searchResult.get(0).getID(), amount);
				Thread.sleep(1000);
				amount = 539.99;
				customer1.placeBidding(searchResult.get(0).getID(), amount);
				Thread.sleep(1000);
				amount = 739.99;
				customer2.placeBidding(searchResult.get(0).getID(), amount);
				*/
			}

			while(true) {
				Thread.sleep(1000);
			}
		} catch (Exception e) {
			System.out.println("fout: store");
			e.printStackTrace();
		}
	}
}
